import React, { Component } from 'react';
import { Layout, Menu, Row } from 'antd';
import { Link } from 'react-router-dom';
import icon from '../../../icon.png';
import './index.css';

const { Item } = Menu;

class Nav extends Component {

  constructor(props) {
    super(props);
    const hash = location.hash.split('/')[1];
    this.state = { current: hash ? hash : 'Home' };
  }

  componentDidUpdate() {
    const items = window.location.href.split('?');
    const urlItems = items[0].split('/')
    let hash = urlItems[urlItems.length-1];
    if (hash.trim().length<=0) {
      hash = 'Home';
    }
    if (this.state.current !== hash) {
      this.setState({
        current: hash
      });
    }
  }

  handleClick = (e) => {
    this.setState({
      current: e.key
    });
  }

  render() {
    return (
      <div style={{ width: 160, height: '100%', zIndex: 999 }}>
        <div className='iconContainer'>
          <img src={icon} className='icon' />
          <span className="titleText">ai-man</span>
        </div>
        <Menu
          defaultSelectedKeys={['1']}
          defaultOpenKeys={['sub1']}
          mode="inline"
          theme="dark"
          inlineCollapsed={this.state.collapsed}
          selectedKeys={[this.state.current]}
          onClick={this.handleClick}
        >
          <Item key="Home">
            <Link to='/Home'>主页</Link>
          </Item>
        </Menu>
      </div>
    );
  }
}

export default Nav;